package es.juanc.reflect;

import java.awt.Rectangle;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class SampleGetField {

	public static void main(String[] args) {
		Rectangle r = new Rectangle(100, 325);
		printHeight(r);
		printFields(r.getClass());
	}

	public static void printFields(Class cl) {
		
		Field[] fields = cl.getDeclaredFields();

		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			Class type = f.getType();
			String name = f.getName();
			System.out.print(Modifier.toString(f.getModifiers()));
			System.out.println(" " + type.getName() + " " + name + ";");
		}
	}

	public static void printHeight(Rectangle r) {
		
		Field heightField;
		Integer heightValue;
		Class c = r.getClass();
		
		try {
			heightField = c.getField("height");
			heightValue = (Integer) heightField.get(r);
			System.out.println("Height: " + heightValue.toString());
		} catch (NoSuchFieldException e) {
			System.out.println(e);
		} catch (SecurityException e) {
			System.out.println(e);
		} catch (IllegalAccessException e) {
			System.out.println(e);
		}
	}
}